package level.editor.tool.tile;

import java.io.File;
import java.io.FilenameFilter;

import javax.swing.JComboBox;

public class ToolTileDirectory extends JComboBox<String>
{
	private File dir;
	
	public ToolTileDirectory(String dir, ToolTile tile)
	{
		super();
		this.dir = new File(dir);
		addItems();
		setSelectedIndex(-1);
		setActionCommand("Directory");
		this.addActionListener(tile);
	}
	
	private void addItems()
	{
		String[] toAdd = fileList();
		if (toAdd != null)
		{
			for (int i = 0; i < toAdd.length; i++)
			{
				this.addItem(toAdd[i]);
			}
		}
	}
	
	private String[] fileList()
	{
		FilenameFilter filter = new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				return !name.endsWith(".txt");
			}
		};
		String[] fileNames = dir.list(filter);
		if (fileNames == null)
		{
			System.out.println("Directory Error");
			return null;
		}
		else
		{
			return fileNames;
		}
	}
	
	public String getDir()
	{
		return dir.getAbsolutePath();
	}
}
